package com.foo.JunitTestCases;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import javax.ejb.EJB;

import org.junit.Assert;
import org.junit.Test;

import com.foo.CompressData.Compress;


public class CompressFileTest {
	
	private Compress objCompress = new Compress();
	public void testStoreFileStringString() {
		String fileSavePath = "";
		String fileName = fileSavePath + "sp500hst.txt";
		String archivePath =fileSavePath;
		//componentLookup();
		File file = new File(archivePath);
		if(file.isDirectory())
		{
			String str[] = file.list();
			
			for(int i =0;i<str.length;i++)
			{
				File f=new File(archivePath + str[i]);
				if(f.isFile())
				{
					f.delete();
				} 
			}
		}
		//Look up for the local Interface of the component
		
		
		objCompress.storeFile(fileName, archivePath);
		int expectedArchievedFiles = 1;
		int actualArchivedFiles = 0;
		File actualfile = new File(archivePath);
		if(actualfile.isDirectory())
		{
			String str[] = actualfile.list();
			
			for(int i =0;i<str.length;i++)
			{
				File f=new File(archivePath + str[i]);
				if(f.isFile())
				{
					if(f.getName().endsWith(".zip"))
					{
						actualArchivedFiles = actualArchivedFiles + 1;
					}
				} 
			}
		}
		Assert.assertEquals(expectedArchievedFiles, actualArchivedFiles);
	}

	@Test
	public void testGetArchiveFileName() {
		String fileSavePath = "";
		String expectedFileName;
		String fromDate = null,toDate = null;
		String strLine = null;
		try
		{
			BufferedReader br = new BufferedReader(new FileReader(fileSavePath + "sp500hst.txt"));
			String s=null;
			if((s=br.readLine())!=null && (s = s.trim()).length() > 0)
			{
				String content[] = s.split(",");
				if(content[0] != null)
				{
					try
					{
						Integer.parseInt(content[0]);
					}
					catch(NumberFormatException e)
					{
						System.out.println(e.getMessage());
					}
					fromDate = content[0];
				}
			}
			while ((s = br.readLine()) != null && (s = s.trim()).length() > 0)
			{
				strLine = s;
			}
			String last = strLine;
			String content_last[] = last.split(",");
			if(content_last[0] != null)
			{
				try
				{
					Integer.parseInt(content_last[0]);
				}
				catch(NumberFormatException e)
				{
					System.out.println(e.getMessage());
				}
				toDate = content_last[0];
			}
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
		}
		expectedFileName = fromDate + "-" + toDate +".zip";
		
		String actualFileName =objCompress.getArchiveFileName(fileSavePath + "sp500hst.txt");
		
		Assert.assertEquals(expectedFileName, actualFileName);
	}

	@Test
	public void testCreateArchiveFile() {
		String fileSavePath = "";
		String fileName = fileSavePath + "sp500hst1.txt";
		String archivePath =fileSavePath;
		String archiveName = "test.zip";
		File file = new File(archivePath);
		
		objCompress.createArchiveFile(fileName, archivePath + archiveName);
		int expectedArchievedFiles = 1;
		int actualArchivedFiles = 0;
		File actualfile = new File(archivePath);
		if(actualfile.isDirectory())
		{
			String str[] = actualfile.list();
			
			for(int i =0;i<str.length;i++)
			{
				File f=new File(archivePath + str[i]);
				if(f.isFile())
				{
					if(f.getName().endsWith(".zip"))
					{
						Assert.assertTrue(f.getName().compareTo("test.zip")== 0);
						actualArchivedFiles = actualArchivedFiles + 1;
					}
				} 
			}
		}
		Assert.assertEquals(expectedArchievedFiles, actualArchivedFiles);
	}

	@Test
	public void testStoreFileByteArrayIntInt() {
		try
		{
			String fileSavePath = "";
			String filePath = fileSavePath;
			int actualFiles = 0,expectedFiles = 0;
			File file = new File(filePath);
			
			if(file.isDirectory())
			{
				String str[] = file.list();
				
				for(int i =0;i<str.length;i++)
				{
					File f=new File(filePath + str[i]);
					if(f.isFile())
					{
						f.delete();
					} 
				}
			}
			String localFilePath = fileSavePath + "sp500hst.txt";
			BufferedReader br = new BufferedReader(new FileReader(localFilePath));
			String s=null;
			StringBuffer stringFileContent = new StringBuffer();
			while((s=br.readLine())!=null && (s = s.trim()).length() > 0)
			{
				stringFileContent.append(s);
			}
			objCompress.storeFile(stringFileContent.toString().getBytes(), 0, 20);
			expectedFiles = 1;
			
			File actualfile = new File(filePath);
			if(actualfile.isDirectory())
			{
				String str[] = actualfile.list();
				
				for(int i =0;i<str.length;i++)
				{
					File f=new File(filePath + str[i]);
					if(f.isFile())
					{
						if(f.getName().endsWith(".txt"))
						{
							actualFiles = actualFiles + 1;
						}
					} 
				}
			}
			
			Assert.assertEquals(expectedFiles, actualFiles);
		}
		catch(Exception e)
		{
			
		}
	}
}
